package com.kxmall.admin.controller.storage;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.storage.domain.bo.WarningStorageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.storage.domain.vo.KxStorageGoodsVo;
import com.kxmall.storage.domain.bo.KxStorageGoodsBo;
import com.kxmall.admin.controller.storage.service.IKxStorageGoodsService;
import com.kxmall.common.core.page.TableDataInfo;

/**
 * 仓库商品
 */
@Api(tags = "仓库商品")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/storage/goods")
public class KxStorageGoodsController extends BaseController {

    private final IKxStorageGoodsService iKxStorageGoodsService;

    /**
     * 查询仓库所有商品列表
     */
    @SaIgnore
    @ApiOperation(value = "/storage/goods/listAll - 查询仓库所有商品列表", httpMethod = "POST")
    @PostMapping("/listAll")
    public R<List<KxStorageGoodsVo>> listAll(KxStorageGoodsBo bo, HttpServletResponse response) {
        List<KxStorageGoodsVo> list = iKxStorageGoodsService.queryList(bo);
        return R.ok(list);
    }
    /**
     * 导出仓库所有商品列表
     */
    @SaCheckPermission("storage:goods:export")
    @Log(title = "导出仓库所有商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "/storage/goods/export - 导出仓库所有商品列表", httpMethod = "POST")
    public void export(KxStorageGoodsBo bo, HttpServletResponse response) {
        List<KxStorageGoodsVo> list = iKxStorageGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库商品", KxStorageGoodsVo.class, response);
    }
    /**
     * 分页查询仓库商品列表
     */
    @SaCheckPermission("storage:goods:pageList")
    @ApiOperation(value = "/storage/goods/pageList - 分页查询仓库商品列表", httpMethod = "GET")
    @GetMapping("/pageList")
    public TableDataInfo<KxStorageGoodsVo> pageList(KxStorageGoodsBo bo, PageQuery pageQuery) {
        return iKxStorageGoodsService.queryPageList(bo, pageQuery);
    }
    /**
     * 获取仓库商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("storage:goods:query")
    @GetMapping("/query/{id}")
    @ApiOperation(value = "/storage/goods/query/{id} - 获取仓库商品详细信息", httpMethod = "GET")
    public R<KxStorageGoodsVo> query(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iKxStorageGoodsService.queryById(id));
    }

    /**
     * 新增仓库商品
     */
    @SaCheckPermission("storage:goods:add")
    @Log(title = "新增仓库商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    @ApiOperation(value = "/storage/goods/add - 新增仓库商品", httpMethod = "POST")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxStorageGoodsBo bo) {
        return toAjax(iKxStorageGoodsService.insertByBo(bo));
    }
    /**
     * 修改仓库商品
     */
    @SaCheckPermission("storage:goods:edit")
    @Log(title = "修改仓库商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    @ApiOperation(value = "/storage/goods/edit - 修改仓库商品", httpMethod = "PUT")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxStorageGoodsBo bo) {
        return toAjax(iKxStorageGoodsService.updateByBo(bo));
    }
    /**
     * 删除仓库商品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("storage:goods:remove")
    @Log(title = "删除仓库商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    @ApiOperation(value = "/storage/goods/remove/{ids} - 删除仓库商品", httpMethod = "DELETE")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iKxStorageGoodsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    /**
     * 上下架
     */
    @PostMapping("/freezeOrActivation")
    @ApiOperation(value = "/storage/goods/freezeOrActivation - 上下架", httpMethod = "POST")
    public R<Boolean> freezeOrActivation(@RequestBody KxStorageGoodsBo bo){
        return R.ok(iKxStorageGoodsService.freezeOrActivation(bo));
    }
    /**
     * 逻辑删除
     */
    @PostMapping("/updateByStorageGoods")
    @ApiOperation(value = "/storage/goods/updateByStorageGoods - 逻辑删除", httpMethod = "POST")
    public R<Boolean> updateByStorageGoods(@RequestBody KxStorageGoodsBo bo){
        return R.ok(iKxStorageGoodsService.updateByStorageGoods(bo));
    }
    /**
     * 更新当前价格
     */
    @PostMapping("/updatePrice")
    @ApiOperation(value = "/storage/goods/updatePrice - 更新当前价格", httpMethod = "POST")
    public R<Boolean> updatePrice(@RequestBody KxStorageGoodsBo bo){
        return R.ok(iKxStorageGoodsService.updatePrice(bo));
    }
    /**
     * 库存预警列表
     */
    @GetMapping("/warningList")
    @ApiOperation(value = "/storage/goods/warningList - 库存预警列表", httpMethod = "GET")
    public TableDataInfo<KxStorageGoodsVo> warningList(WarningStorageBo bo, PageQuery pageQuery) {
        return iKxStorageGoodsService.queryPageWarningList(bo, pageQuery);
    }
    /**
     * 设置预警量
     */
    @PostMapping("/warningUpdate")
    @ApiOperation(value = "/storage/goods/warningUpdate - 设置预警量", httpMethod = "POST")
    public R<Boolean> warningUpdate(@RequestBody WarningStorageBo bo) {
        return R.ok(iKxStorageGoodsService.warningUpdate(bo));
    }


}
